<?php
namespace app\api\controller;

class PayRecord extends Permission
{
    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $limit = isset($this->input_data['limit']) ? $this->input_data['limit'] : 15;
        $map = [];

        if( isset($this->input_data['relation_id']) && $this->input_data['relation_id'] != '') $map['relation_id'] = $this->input_data['relation_id'];

        if( isset($this->input_data['type']) && $this->input_data['type'] != '全部') $map['type'] = ['LIKE','%'.$this->input_data['type'].'%'];
        if( isset($this->input_data['cate']) && $this->input_data['cate'] != '全部') $map['cate'] = ['LIKE','%'.$this->input_data['cate'].'%'];
        $map['status'] = $this->post('status');

        $total = db('PayRecord')->where($map)->count();
        $rows = db('PayRecord')->order('id', 'DESC')->page($page,$limit)->where($map)->select();
        $sql = db()->getLastSql();
        $list = [];
        foreach ($rows as $v) {

            $v['amount'] = formatMoneyShow($v['amount']);

            $v['createTime'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['payTime'] = date('Y-m-d H:i:s',$v['pay_time']);
            $v['judgeTime'] = date('Y-m-d H:i:s',$v['judge_time']);
            
            $list[] = $v;
        }

        $map['status'] = 0;
        $judgeCount = db('PayRecord')->where($map)->count();

        $badgeData = [
            'judge' => $judgeCount
        ];
        
        $data = [
            'total' => $total,
            'list' => $list,
            'badgeData' => $badgeData,
            'sql' => $this->post('status')
        ];
        $this->resSuc($data);
    }
  
    public function getById()
    {
        $payRecordId = $this->post('id');
        $detail = db('PayRecord')->where(['id'=>$payRecordId])->find();
        $detail['amount'] = formatMoneyShow($detail['amount']);
        $detail['pay_time'] = date('Y-m-d H:i:s',$detail['pay_time']);

        $relationData = [];
        $relationData = $this->_getRelationInsurance($payRecordId,$this->post('relationType'));


        $data = [
            'payRecord' => $detail,
            'relationData' => $relationData
        ];

        $this->resSuc($data);
    }

    private function _getRelationInsurance($payRecordId,$type)
    {
        if($type == '') return [];
        if($type == '返点收入') $map = ['rebate_pay_record_id' => $payRecordId];
        if($type == '保险结账') $map = ['pay_record_id' => $payRecordId];
        
        $rows = db('Insurance')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->field('a.*,b.plate_number,b.company,b.area')->order('id', 'desc')->where($map)->select();

        $list = [];

        foreach ($rows as $v) {
            
            # 总价，强险的加车船税进去，中润的加加价进去
            $v['totalPrice'] = formatMoneyShow( bcadd( bcadd($v['price'],$v['tax_price']), $v['raise_price']) );
            # 加价或者车船税
            $otherPrice = $v['cate'] == '交强险' ? $v['tax_price'] : $v['raise_price'];
            $v['otherPrice'] = formatMoneyShow($otherPrice);

            $v['pay_amount'] = formatMoneyShow($v['pay_amount']);
            $v['price'] = formatMoneyShow($v['price']);
            $v['raise_price'] = formatMoneyShow($v['raise_price']);
            $v['tax_price'] = formatMoneyShow($v['tax_price']);
            $v['rebate_amount'] = formatMoneyShow($v['rebate_amount']);
            $v['rebate_amount_paid'] = formatMoneyShow($v['rebate_amount_paid']);

            $v['rebate_pay_time'] = $v['rebate_pay_time'] > 0 ? date('Y-m-d H:i:s',$v['rebate_pay_time']) : '';
            $v['rebate_time'] = $v['rebate_time'] > 0 ? date('Y-m-d H:i:s',$v['rebate_time']) : '';
            $v['createDate'] = $v['create_time'] > 0 ? date('Y-m-d H:i',$v['create_time']) : '';
            $v['rebate'] = $v['rebate'] * 1;
            $v['payImages'] = json_decode($v['pay_images'],true);
            $v['rebateImages'] = json_decode($v['rebate_images'],true);
            $v['offerImages'] = json_decode($v['images'],true);

            $list[] = $v;
        }

        return $list;
    }

}
